//
//  XYNewFeatureController.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 15/12/30.
//  Copyright © 2015年 cn.weibo. All rights reserved.
//

import UIKit

class XYNewFeatureController: UIViewController,UIScrollViewDelegate {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
    }
    private func setUpUI() {
        
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        // 添加约束
        scrollView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(view)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_bottom).offset(-50)
        }
        // 循环添加图片
        let imageCount = 4
        for i in 0 ..< imageCount {
            
            let image = UIImage(named: "new_feature_\(i + 1)")
            let imageView = UIImageView(image: image)
            imageView.frame = CGRectMake(CGFloat(i) * kSCREENWITH, 0, kSCREENWITH, kSCREENHEIGHT)
            scrollView.addSubview(imageView)
            if i == imageCount - 1 {
                
                setUpLastPage(imageView)
            }
        }
        scrollView.contentSize = CGSizeMake(CGFloat(imageCount) * kSCREENWITH, 0)
        
    }
    private func setUpLastPage(imageView:UIImageView) {
        
        imageView.userInteractionEnabled = true
        imageView.addSubview(shareBtn)
        imageView.addSubview(enterBtn)
        // 添加约束
        enterBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imageView.snp_centerX)
            make.centerY.equalTo(imageView.snp_bottom).offset(-150)
        }
        
        shareBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(enterBtn)
            make.bottom.equalTo(enterBtn.snp_top).offset(-20)
        }
        
    }
    // 懒加载控件
    private lazy var enterBtn: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setTitle("进入微博", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
    @objc private func enterButtonClick() {
        
        // 跳转控制器
        NSNotificationCenter.defaultCenter().postNotificationName(changeRootViewControllerNotification, object: nil)
        
    }
    private lazy var shareBtn: UIButton = {
        
        let shareBtn = UIButton()
        shareBtn.addTarget(self, action: "sharedButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置不同状态的图片
        shareBtn.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        shareBtn.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        // 设置文字以及文字的属性
        shareBtn.setTitle("分享到微博", forState: UIControlState.Normal)
        shareBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        shareBtn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 调整边距
        shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5)
        return shareBtn
    }()
    @objc private func sharedButtonClick(sender:UIButton) {
        
        shareBtn.selected = !shareBtn.selected
        
    }
    private lazy var scrollView: UIScrollView = {
        
        let scrollView = UIScrollView()
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        return scrollView
    }()
    private lazy var pageControl: UIPageControl = {
        
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        pageControl.numberOfPages = 4
        return pageControl
    }()
}
// MARK: 代理
extension XYNewFeatureController {
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / kSCREENWITH + 0.5)
        pageControl.currentPage = page
        
    }
    
}